import cv2 as cv
import numpy as np
import os
import sys
from python_ai.common.xcommon import *

VER = 'v1.0'
FILE_NAME = os.path.basename(__file__)
SAVE_DIR = os.path.join('_save', FILE_NAME, VER)
os.makedirs(SAVE_DIR, exist_ok=True)
FPS = 25
INTERVAL = 1000 // FPS

video_path = '../../../../large_data/video/cam/save_blue_video.avi'
save_path = os.path.join(SAVE_DIR, rand_name_on_now() + '.avi')

input = cv.VideoCapture(video_path)
w = int(input.get(cv.CAP_PROP_FRAME_WIDTH))
h = int(input.get(cv.CAP_PROP_FRAME_HEIGHT))
print(w, h)
fourcc = cv.VideoWriter_fourcc(*list('XVID'))
output = cv.VideoWriter(save_path, fourcc, FPS, (w, h))

print('Press ESC to force exit.')
while True:
    ret, img = input.read()
    if not ret:
        print('Video over.')
        break

    cv.imshow('video', img)
    output.write(img)
    k = cv.waitKey(INTERVAL) & 0xFF
    if 27 == k:
        print('Forced exit.')
        break

input.release()
output.release()
cv.destroyAllWindows()
